/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.modular.xygenealogy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.xygenealogy.entity.XyGenealogy;
import vip.xiaonuo.modular.xygenealogy.enums.XyGenealogyExceptionEnum;
import vip.xiaonuo.modular.xygenealogy.form.XyGenealogyForm;
import vip.xiaonuo.modular.xygenealogy.mapper.XyGenealogyMapper;
import vip.xiaonuo.modular.xygenealogy.param.XyGenealogyParam;
import vip.xiaonuo.modular.xygenealogy.service.XyGenealogyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.modular.xygenealogy.vo.GroupGenealogyVo;
import vip.xiaonuo.modular.xygenealogycate.entity.XyGenealogyCate;
import vip.xiaonuo.modular.xygenealogycate.mapper.XyGenealogyCateMapper;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 谱文service接口实现类
 *
 * @author yanwei
 * @date 2023-05-05 15:44:28
 */
@Service
public class XyGenealogyServiceImpl extends ServiceImpl<XyGenealogyMapper, XyGenealogy> implements XyGenealogyService {


    @Resource
    private XyGenealogyCateMapper xyGenealogyCateMapper;


    @Override
    public PageResult<XyGenealogy> page(XyGenealogyParam xyGenealogyParam) {
        QueryWrapper<XyGenealogy> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(xyGenealogyParam)) {

            // 根据类目编号 查询
            if (ObjectUtil.isNotEmpty(xyGenealogyParam.getCateId())) {
                queryWrapper.lambda().eq(XyGenealogy::getCateId, xyGenealogyParam.getCateId());
            }
            // 根据类目名称 查询
            if (ObjectUtil.isNotEmpty(xyGenealogyParam.getCateName())) {
                queryWrapper.lambda().eq(XyGenealogy::getCateName, xyGenealogyParam.getCateName());
            }
            // 根据家谱编号 查询
            if (ObjectUtil.isNotEmpty(xyGenealogyParam.getLogyId())) {
                queryWrapper.lambda().eq(XyGenealogy::getLogyId, xyGenealogyParam.getLogyId());
            }
            // 根据标题 查询
            if (ObjectUtil.isNotEmpty(xyGenealogyParam.getTitle())) {
                queryWrapper.lambda().eq(XyGenealogy::getTitle, xyGenealogyParam.getTitle());
            }
            // 根据图片封面 查询
            if (ObjectUtil.isNotEmpty(xyGenealogyParam.getImgUrl())) {
                queryWrapper.lambda().eq(XyGenealogy::getImgUrl, xyGenealogyParam.getImgUrl());
            }
            // 根据谱文富文本 查询
            if (ObjectUtil.isNotEmpty(xyGenealogyParam.getContent())) {
                queryWrapper.lambda().eq(XyGenealogy::getContent, xyGenealogyParam.getContent());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<XyGenealogy> list(XyGenealogyParam xyGenealogyParam) {
        return this.list();
    }

    @Override
    public void add(XyGenealogyParam xyGenealogyParam) {
        XyGenealogy xyGenealogy = new XyGenealogy();
        BeanUtil.copyProperties(xyGenealogyParam, xyGenealogy);
        this.save(xyGenealogy);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<XyGenealogyParam> xyGenealogyParamList) {
        xyGenealogyParamList.forEach(xyGenealogyParam -> {
            this.removeById(xyGenealogyParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(XyGenealogyParam xyGenealogyParam) {
        XyGenealogy xyGenealogy = this.queryXyGenealogy(xyGenealogyParam);
        BeanUtil.copyProperties(xyGenealogyParam, xyGenealogy);
        this.updateById(xyGenealogy);
    }

    @Override
    public XyGenealogy detail(XyGenealogyParam xyGenealogyParam) {
        return this.queryXyGenealogy(xyGenealogyParam);
    }

    /**
     * 获取谱文
     *
     * @author yanwei
     * @date 2023-05-05 15:44:28
     */
    private XyGenealogy queryXyGenealogy(XyGenealogyParam xyGenealogyParam) {
        XyGenealogy xyGenealogy = this.getById(xyGenealogyParam.getId());
        if (ObjectUtil.isNull(xyGenealogy)) {
            throw new ServiceException(XyGenealogyExceptionEnum.NOT_EXIST);
        }
        return xyGenealogy;
    }

    @Override
    public void export(XyGenealogyParam xyGenealogyParam) {
        List<XyGenealogy> list = this.list(xyGenealogyParam);
        PoiUtil.exportExcelWithStream("SnowyXyGenealogy.xls", XyGenealogy.class, list);
    }

    /**
     * 添加谱文
     * @param xyGenealogyForm
     */
    @Override
    public void genealogySave(XyGenealogyForm xyGenealogyForm) {
        XyGenealogyCate xyGenealogyCate = xyGenealogyCateMapper.selectById(xyGenealogyForm.getCateId());
        Assert.isTrue(Objects.nonNull(xyGenealogyCate),"对应分类未找到!");
        XyGenealogy xyGenealogy = getById(xyGenealogyForm.getId());
        if (Objects.nonNull(xyGenealogy)){
            BeanUtil.copyProperties(xyGenealogyForm,xyGenealogy);
            updateById(xyGenealogy);
        }else{
            xyGenealogy = new XyGenealogy();
            BeanUtil.copyProperties(xyGenealogyForm,xyGenealogy);
            xyGenealogy.setCateName(xyGenealogyCate.getCateName());
            save(xyGenealogy);
        }


    }

    @Override
    public List<GroupGenealogyVo> groupGenealogyList(String logyId) {
        // 不查询富文本信息
        List<XyGenealogy> genealogyList = list(Wrappers.<XyGenealogy>lambdaQuery().select(XyGenealogy.class,i -> !"content".equals(i.getProperty())).eq(XyGenealogy::getLogyId, logyId));
        Map<String, List<XyGenealogy>> stringListMap = genealogyList.stream().collect(Collectors.groupingBy(XyGenealogy::getCateId));
        List<GroupGenealogyVo> groupGenealogyVos = Lists.newArrayList();
        stringListMap.forEach((key,val)->{
            GroupGenealogyVo groupGenealogyVo = new GroupGenealogyVo();
            groupGenealogyVo.setXyGenealogyList(val);
            groupGenealogyVo.setCateId(key);
            groupGenealogyVo.setCateName(xyGenealogyCateMapper.selectById(key).getCateName());
            groupGenealogyVos.add(groupGenealogyVo);
        });
        return groupGenealogyVos;
    }

}
